package dao.impl;

import dao.inter.MaintowerDeformation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.DBGetDataUtil;
import utils.Pub;

import java.sql.SQLException;

public class MaintowerDeformationImpl implements MaintowerDeformation {
    public static final Logger logger = LogManager.getLogger(MaintowerDeformationImpl.class.getName());
    private static final String dataTable = "MaintowerDeformation";

    public JSONObject getJsonResultByDateAndSersors(String red,String yellow,String bridgeType,String beginDate, String endDate, String sersors) throws Exception {
        JSONObject result = new JSONObject();
        JSONArray resultArray = new JSONArray();
        String[] sersorArray = sersors.split(",");
        String sql1 = null;
        try {
            for (String aSersorArray : sersorArray) {
                sql1 = "select TIME,MAX,MIN,AVG,RED,YELLOW from " + dataTable + "  where TIME between ? and ? AND SERSORS = ? order by TIME";
                JSONObject data = DBGetDataUtil.getJsonDataForMaintower(red,yellow,sql1,dataTable, beginDate, endDate, aSersorArray);
                data.put(Pub.SENSORID, aSersorArray);
                resultArray.add(data);
            }
        } catch (SQLException e) {
            result.put(Pub.STATUS, false);
            throw new Exception(e.getMessage());
        }
        result.put("type", bridgeType);
        result.put(Pub.STATUS, true);
        result.put(Pub.DATA, resultArray);
        return result;
    }

}
